package com.ulic.customerManApplication.usermodule.action;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseAction;
import com.ulic.customerManApplication.commonmodule.service.LogService;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.usermodule.bo.Adminitrator;
import com.ulic.customerManApplication.usermodule.service.AdminitratorService;
import com.ulic.customerManApplication.util.md5.MD5;

@Component("adminLoginAction")
@Scope("prototype")
public class AdminLoginAction extends BaseAction{

	
	private AdminitratorService adminitratorService;
	
	private LogService logService;
	
	private String username;
	
	private String password;
	
	private Logger logger = Logger.getLogger(AdminLoginAction.class);
	
	@Resource
	public void setAdminitratorService(AdminitratorService adminitratorService) {
		this.adminitratorService = adminitratorService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	@Resource
	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public String adminlogin() throws Exception {
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest(); 
		Adminitrator admin = new Adminitrator();
		password = MD5.getMD5Digest(password);
		try {
			admin = adminitratorService.findUserByPwd(username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String content = "";
		if(admin == null) {
			content = "超级管理员：" + username + " 尝试登录失败，原因是：用户名或密码错误！";
		} else {
			content = "超级管理员：" + username + " 尝试登录成功！";
		}
		logService.addLog(httpServletRequest.getRemoteAddr().toString(),new Date(),CONSTANT.LOGIN_LOG_TYPE,content);
		logger.debug(content);
		if(admin==null){
			request.put("message", content);
			return "error";
		}
		session.put(CONSTANT.ADMIN_SESSION_KEY, admin);
		adminrecache();
		return SUCCESS;
	}
	
	/**
	 * Description: 
	 * @Version1.0 
	 * 2012-7-11 下午01:06:32 李求智 (liqz@huilan.com) created
	 * @return
	 * @throws Exception
	 */
	public String admin() throws Exception{
		return SUCCESS;
	}

	
	public static void main(String[] args){
		System.out.println(MD5.getMD5Digest("1"));
	}
}
